import { Component, OnInit, ViewChild } from '@angular/core';
import { NavController, IonAccordionGroup,AlertController } from '@ionic/angular';
@Component({
  selector: 'app-d06',
  templateUrl: './d06.page.html',
  styleUrls: ['./d06.page.scss'],
})
export class D06Page implements OnInit {

  constructor(public navCtrl: NavController, public alertCtrl:AlertController) { }
  @ViewChild(IonAccordionGroup, { static: true }) accordionGroup: IonAccordionGroup;

  ngOnInit() {
  }

  async showAlert(message: string) {
    let alert = this.alertCtrl.create({
      buttons: ['确定'],
      header:message
    });
    (await alert).present();
  }


  click1() {
    let map = new BMap.Map("bdmap");
    map.enableScrollWheelZoom();
    let point = new BMap.Point(116.331398, 39897445);
    map.centerAndZoom(point, 12);

    let geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition((r) => {
      if (geolocation.getStatus() == BMAP_STATUS_SUCCESS) {
        let mk = new BMap.Marker(r.point);
        map.addOverlay(mk);
        map.panTo(r.point);
        this.showAlert("您的位置:" + r.point.lng + "," + r.point.lat);
      }
      else {
        this.showAlert('failed' + geolocation.getStatus());
      }
    }, { enableHighAccuracy: true })
    this.accordionGroup.value = undefined;
  }

  click2() {
    let map = new BMap.Map("bdmap");
    map.enableScrollWheelZoom();
    let point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12);

    let myFun = (result) => {
      let cityName = result.name;
      map.setCenter(cityName);
      this.showAlert("当前定位城市:" + cityName);
    }
    let myCity = new BMap.LocalCity();
    myCity.get(myFun);
    this.accordionGroup.value = undefined;
  }

  click3() {
    let myGeo = new BMap.Geocoder();
    let city = "开封市";
    myGeo.getPoint(city, (p)=> {
      let map = new BMap.Map("bdmap");
      map.enableScrollWheelZoom();
      if (p) {
        let marker = new BMap.Marker(p);
        map.addOverlay(marker);
        map.centerAndZoom(p, 11);
      } else {
        let point = new BMap.Point(116.331398, 39.897445);
        let marker = new BMap.Marker(point);
        map.addOverlay(marker);
        map.centerAndZoom(point, 11);

      }
    }, city)
    this.accordionGroup.value = undefined;
  }

  click4() {
    let map = new BMap.Map("bdmap");
    map.centerAndZoom(new BMap.Point(116.331398, 39.897445), 11);
    map.enableScrollWheelZoom();

    let MyLocation = (longitude: number, latitude: number) => {
      map.clearOverlays();
      let p = new BMap.Point(longitude, latitude);
      let marker = new BMap.Marker(p);
      map.addOverlay(marker);
      map.panTo(p);
    }
    MyLocation(114.315745, 34.824635);
    this.accordionGroup.value = undefined;
  }

}
